package cn.zyzpp.controller.main;

import cn.zyzpp.entity.job.JobClass;
import cn.zyzpp.entity.job.NewJob;
import cn.zyzpp.service.job.JobService;
import cn.zyzpp.service.mail.MailUtil;
import cn.zyzpp.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;

/**
 * Create by yster@foxmail.com 2018/5/22/022 21:15
 */
@Controller
public class MainController {
    @Autowired
    private JobService jobService;
    @Autowired
    private UserService userService;
    @Autowired
    private MailUtil mailUtil;

    @RequestMapping("/")
    public String index(ModelMap model,@RequestParam(defaultValue = "0") Integer id){
        //获取最新列表
        List<NewJob> jobs = jobService.queryByPageNoClose(id).getContent();
        for (NewJob job: jobs) {
            String s = userService.queryUserImg(job.getUid()).getImgUrl();
            job.setImgUrl(s);
        }
        model.addAttribute("jobs",jobs);
        //获取最热列表
        List<NewJob> jobt = jobService.queryHotByPage(id).getContent();
        for (NewJob job: jobt) {
            String s = userService.queryUserImg(job.getUid()).getImgUrl();
            job.setImgUrl(s);
        }
        model.addAttribute("joblist",JobClass.getJobList());
        model.addAttribute("jobt",jobt);
        return "index";
    }

    @RequestMapping("/soso")
    public String soso(String place, String name,@RequestParam(defaultValue = "0") Integer id ,ModelMap model){
        //获取最新列表
        Page<NewJob> newJobs;
        if ("全部类别".equals(place)){
            newJobs = jobService.queryByNameAndPage(name,id);
        }else {
            newJobs = jobService.queryByNameAndSortAndPage(name,place,id);
        }
        List<NewJob> jobs =newJobs.getContent();
        int pgnums = newJobs.getTotalPages();//分页总数
        int pgnum = newJobs.getNumber();//当前第几页
        long number = newJobs.getTotalElements();//总的元素数
        for (NewJob job: jobs) {
            String s = userService.queryUserImg(job.getUid()).getImgUrl();
            job.setImgUrl(s);
        }
        model.addAttribute("place",place);
        model.addAttribute("name",name);
        model.addAttribute("number",number);
        model.addAttribute("pgnums", pgnums);
        model.addAttribute("pgnum", pgnum);
        model.addAttribute("jobs",jobs);
        model.addAttribute("joblist",JobClass.getJobList());
        return "soso";
    }

    @ResponseBody
    @RequestMapping(value = "/forgetPassword",method = RequestMethod.POST)
    public String forgetPassword(String mail){
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i=0;i<6;i++){
            stringBuffer.append(random.nextInt(10));
        }
        try {
            userService.updataPassWord(mail,stringBuffer.toString());
        }catch (Exception e){
            return "此邮箱账号不存在";
        }
        try {
            mailUtil.sendMail(mail,"密码已重置","你的新密码为："+stringBuffer.toString());
        } catch (MessagingException e) {
            System.out.println(e.getMessage());
            return "邮件发送器配置失败";
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
            return "邮件发送器配置失败";
        }
        return "新密码已发送到您的邮箱";
    }

    @RequestMapping("/forget")
    public String forgetPage(){
        return "forget";
    }
}
